package com.example.newland.application;

import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.nle.mylibrary.claimer.rfid.SingleEpcListener;
import com.nle.mylibrary.databus.DataBus;
import com.nle.mylibrary.databus.DataBusFactory;
import com.nle.mylibrary.databus.ReciveData;
import com.nle.mylibrary.device.RFID;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView main_tv_exit;
    private Button main_but_du;
    private Button main_but_save;
    private TextView main_tv_epc;
    //private DataBus dataBus;
    //private RFID rfid;
    private SharedPreferences config;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        main_tv_exit = findViewById(R.id.main_tv_exit);
        main_but_du = findViewById(R.id.main_but_du);
        main_but_save = findViewById(R.id.main_but_save);
        main_tv_epc = findViewById(R.id.main_tv_epc);
        main_tv_exit.setOnClickListener(this);
        main_but_du.setOnClickListener(this);
        main_but_save.setOnClickListener(this);

//        dataBus = DataBusFactory.newSocketDataBus("172.18.3.15", 6001);
//
//        dataBus.setReciveDataListener(new ReciveData() {
//            @Override
//            public String getReciveData(byte[] bytes) {
//                return null;
//            }
//        });

        config = getSharedPreferences("config", MODE_PRIVATE);

    }

    @Override
    public void onClick(View view) {
        switch(view.getId()){
            case R.id.main_tv_exit:
                finish();
                break;
            case R.id.main_but_du:
                try {
                    EpcInit.epc1().readSingleEpc(new SingleEpcListener() {
                        @Override
                        public void onVal(String s) {
                            Log.e("EPC",s);
                            main_tv_epc.setText(s);

                        }
                        @Override
                        public void onFail(Exception e) {
                            e.printStackTrace();
//                            Log.i("main", e.getMessage() != null ? e.getMessage():"2341234");
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }

                break;
            case R.id.main_but_save:
                String StrEpc = main_tv_epc.getText().toString();
                if(StrEpc != null && !"".equals(StrEpc)){
                    SharedPreferences.Editor edit = config.edit();
                    edit.putString(StrEpc,StrEpc);
                    edit.commit();

                    Toast.makeText(this, "激活成功！", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }
}
